﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace XiaoYuStock.Core.Entities
{
    public class TransferDataHelper
    {
        /// <summary>
        /// 转换为数据对象
        ///  string s = "var hq_str_sh601006=\"大秦铁路,8.290,8.280,8.490,8.530,8.270,8.480,8.490,60749173,512260250.000,38431,8.480,95370,8.470,186700,8.460,119271,8.450,159328,8.440,11000,8.490,396245,8.500,304900,8.510,282512,8.520,406878,8.530,2016-01-06,15:00:00,00\"";
        ///var hq_str_sh600035="楚天高速,6.120,5.570,5.860,6.120,5.760,5.850,5.860,61430048,366352323.000,623944,5.850,75000,5.840,169700,5.830,630100,5.820,533000,5.810,249680,5.860,557900,5.870,260835,5.880,404556,5.890,297600,5.900,2016-08-02,13:56:42,00";
        ///
        /// </summary>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static TransferData ConvertToObj(string Content)
        {

            TransferData transData = new TransferData();
            if (string.IsNullOrWhiteSpace(Content))
                return transData;
            Regex reg_Content = new Regex(@".*?hq_str.*?(\d+)=""(.*?)""", RegexOptions.IgnoreCase);
            if (!reg_Content.IsMatch(Content))
                return transData;
            var m=reg_Content.Match(Content);
            return ConvertToObj(m.Groups[1].Value,m.Groups[2].Value.Split(','));
        }
        public static List<TransferData> ConvertToObjs(string Content)
        {
            List<TransferData> result = new List<TransferData>();
            Regex reg_Content = new Regex(@".*?hq_str_(.*?\d+)=""(.*?)""", RegexOptions.IgnoreCase);
            if (!reg_Content.IsMatch(Content))
                return result;
            var ms = reg_Content.Matches(Content);
            foreach(Match m in ms)
            {
                TransferData t = ConvertToObj(m.Groups[1].Value, m.Groups[2].Value.Split(','));
                result.Add(t);
            }
            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Code"></param>
        /// <param name="ArrayData"></param>
        /// <returns></returns>
        private static TransferData ConvertToObj(string Code, string[] ArrayData)
        {
            TransferData transData = new TransferData();
            if (ArrayData == null || ArrayData.Length <= 1)
                return transData;
            transData.SharesSourceCode = Code;
            Regex c = new Regex(@"\d+");
            transData.SharesCode = c.Match(Code).Value;
            transData.SharesName = ArrayData[0];
            transData.TodayOpenPrice = Convert.ToDecimal(ArrayData[1]);
            transData.YesterdayClosePrice = Convert.ToDecimal(ArrayData[2]);
            transData.CurrentPrice = Convert.ToDecimal(ArrayData[3]);
            transData.TodayTopPrice = Convert.ToDecimal(ArrayData[4]);
            transData.TodayBomPrice = Convert.ToDecimal(ArrayData[5]);

            transData.TradeCount = Convert.ToInt32(ArrayData[8]);
            transData.TradeAmount = Convert.ToDecimal(ArrayData[9]);
            transData.Date = ArrayData[30];
            transData.Time = ArrayData[31];
            return transData;
        }
        /// <summary>
        /// 返回股票数据
        /// </summary>
        /// <param name="sharesCode"></param>
        /// <returns></returns>
        public static TransferData LoadTransferData(string sharesCode)
        {
            string url = "http://hq.sinajs.cn/list=" + sharesCode;
            string htmlContent = RequestHelp.Get(url, Encoding.GetEncoding("GB2312"));
            return ConvertToObj(htmlContent);
        }
        /// <summary>
        /// 返回股票数据
        /// </summary>
        /// <param name="sharesCode"></param>
        /// <returns></returns>
        public static List<TransferData> LoadTransferDataItems(string sharesCode)
        {
            string url = "http://hq.sinajs.cn/list=" + sharesCode;
            string htmlContent = RequestHelp.Get(url, Encoding.GetEncoding("GB2312"));
            return ConvertToObjs(htmlContent);
        }

        /// <summary>
        ///  http://hq.sinajs.cn/etag.php?list=sh600035
        /// </summary>
        /// <returns></returns>
        public static TransferData LoadImmediateData(string sharesCode)
        {
            string url = "http://hq.sinajs.cn/etag.php?list=" + sharesCode;
            string htmlContent = RequestHelp.Get(url);
            return ConvertToObj(htmlContent);
        }
    }
}
